﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.Views;
using CommunitySystem.Ctrls;
using CommunitySystem.Common;
using System.Collections.ObjectModel;
using System.Linq;
using System.Xml.Linq;

namespace CommunitySystem.PageView.AdvancedCustomerQuery
{
    public partial class ColumnsInSelectControl :  AsyncCtrl, IReturnListenerc
    {
        private ObservableCollection<CommunitySystem.CustomerQuery.Query_ShowColumn> columns = null;
        private XElement _element = null;
        public ColumnsInSelectControl(XElement element)
        {
            InitializeComponent();
            _element = element;
            columns = QueryViewColumns.GetAllCollumns();
            Loaded += new RoutedEventHandler(ColumnsInSelectControl_Loaded);
        }

        void ColumnsInSelectControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (columns != null)
            {
                foreach (var c in columns)
                {
                    CheckBox cb = new CheckBox() { Name = c.ColumnCode, Content = c.ColumnName, Margin = new Thickness(10, 5, 0, 0), FontSize = 14, Tag= c };
                    wp.Children.Add(cb);
                }
            }
            //默认全选
            selected.IsChecked = true;
            CheckBox_Click(selected, null);
        }

        public void BeginReturn()
        {
            return;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CheckBox_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	// 在此处添加事件处理程序实现。
            RadioButton rb = selected as RadioButton;
            if (rb != null)
            {
                bool IsCheck = false;
                switch (rb.Name)
                {
                    case "selected":
                        if (rb.IsChecked == true)
                        {
                            IsCheck = true;
                        }
                        break;
                    case "unselected":
                        if (rb.IsChecked == true)
                        {
                            IsCheck = true;
                        }
                        break;
                }
                foreach (var ui in wp.Children)
                {
                    CheckBox cb = ui as CheckBox;
                    if (cb != null)
                    {
                        cb.IsChecked = IsCheck;
                    }
                }
            }
        }

        private void ImageButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	// 在此处添加事件处理程序实现。
            List<CustomerQuery.Query_ShowColumn> list = new List<CustomerQuery.Query_ShowColumn>();
            foreach (var ui in wp.Children)
            {
                CheckBox cb = ui as CheckBox;
                if (cb != null && cb.IsChecked == true)
                {
                    list.Add(cb.Tag as CustomerQuery.Query_ShowColumn);
                }
            }

            if (list.Count == 0)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("请至少选中一个字段");
                return;
            }
                
               

                    
                    DetailQueryResultControl view = new DetailQueryResultControl(_element, list.ToArray());
                    Nav.ShowNextPage("查询结果", view, this);
               

            
            
        }
    }
}
